package tgc.edu.library.web.controller;

import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import tgc.edu.library.custom.AjaxResult;
import tgc.edu.library.custom.CommonController;
import tgc.edu.library.custom.EasyuiResult;
import tgc.edu.library.entity.Book;
import tgc.edu.library.entity.BookType;
import tgc.edu.library.service.BookService;
import tgc.edu.library.service.BookTypeService;
import tgc.edu.library.web.form.BookForm;

@Controller
@RequestMapping(value="/book")
public class BookController extends CommonController<Book, Integer, BookForm>{
	@Autowired
	private BookService bookService;
	@Autowired
	private BookTypeService booktypeService;
	
	
	@Override
	public Specification<Book> buildSpec(BookForm form) {
		Specification<Book> specification=new Specification<Book>() {

			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				String search = form.getSearch();
				if(StringUtils.hasText(search)) {
					Predicate bookName = cb.like(root.get("bookName"), "%"+search+"%");
					Predicate intro = cb.like(root.get("intro"), "%"+search+"%");
					Predicate quantity = cb.like(root.get("quantity"), "%"+search+"%");
					Predicate bookCode = cb.like(root.get("bookCode"), "%"+search+"%");
					Predicate type = cb.like(root.get("booktype").get("type"), "%"+search+"%");
					Predicate or = cb.or(bookName, intro,quantity,bookCode,type);
					rules.add(or);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return specification;
	}
	
	@RequestMapping(value="/bookAll")
	public Object bookAll(ModelMap map) {
		List<Book> booklist = bookService.findAll();
		List<BookType> booktypelist = booktypeService.findAll();
		map.put("booklist", booklist);
		map.put("booktypelist", booktypelist);
		return "user/bookAll";
	}
	
	@RequestMapping(value="/edit")
	public void edit(Integer id,ModelMap map) {
		Book model = new Book();
		List<BookType> list =  booktypeService.findAll();
		if(id!=null) {
			model = bookService.findById(id);
		}
		map.put("model", model);
		map.put("list", list);
	}
	
	@RequestMapping(value = "/upload")
	@ResponseBody
	private Object upload(MultipartFile file, ModelMap map) {
		String uuid = UUID.randomUUID().toString();
		boolean boole = bookService.savefile(file, uuid);
		if (boole) {
			return new AjaxResult(true, uuid);
		} else {
			return new AjaxResult(true, file.getOriginalFilename());
		}

	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(BookForm form) {
		Book model = bookService.findByUuid(form.getUuid());
		Integer id= form.getId();
		BookType type = booktypeService.findById(form.getBooktypeId().getId());
		if(id!=null) {
			model = bookService.findById(id);
		}
		model.setBookName(form.getBookName());
		model.setBookCode(form.getBookCode());
		model.setQuantity(form.getQuantity());
		model.setBooktype(type);
		model.setIntro(form.getIntro());
		model.setPress(form.getPress());
		model.setAuthor(form.getAuthor());
		bookService.save(model);
		return new EasyuiResult("数据保存成功");
	}
	
	/**
	 * 下载
	 * @param uuid
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/download")
	@ResponseBody
	private void download(String uuid, HttpServletRequest request, HttpServletResponse response) {
		bookService.download(uuid, request, response);
	}
}
